package com.winxuan.channel.service.commodity.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.winxuan.channel.model.commodity.CommoditySnap;
import com.winxuan.channel.model.commodity.CommoditySnapPK;
import com.winxuan.channel.service.commodity.SnapService;
import com.winxuan.support.dao.BaseDao;
import com.winxuan.support.dao.Finder;
import com.winxuan.support.dao.ParametersFinder;
import com.winxuan.util.StringUtils;
import com.winxuan.util.pagination.Pagination;

@Service("snapService")
public class SnapServiceImpl implements SnapService {

	@Override
	public List<CommoditySnap> getCommoditySnapByDistribute(Integer distribute,
			Integer commodity, Pagination pagination) {
		final Map<String,Integer> params = new HashMap<String,Integer>();
		params.put("distribute", distribute);
		params.put("commodity", commodity);
		Finder finder = new ParametersFinder(params) {
			@Override
			public String genQueryString() {
				StringBuffer sb=new StringBuffer("SELECT s FROM CommoditySnap s WHERE 1=1");
				if(params.get("distribute") != -1){
					sb.append(" AND s.commoditySnapPK.distribute=:distribute ");
				}
				if(params.get("commodity") != 0){
					sb.append(" AND s.commoditySnapPK.commodity=:commodity ");
				}
				return sb.toString();
			}
		};
		return baseDao.find(finder,pagination);
	}

	@Override
	public boolean addCommoditySnap(CommoditySnap commoditySnap) {
		CommoditySnapPK snapPK = (CommoditySnapPK) baseDao.save(commoditySnap);
		return snapPK != null;
	}

	@Override
	public boolean updateCommoditySnap(CommoditySnap commoditySnap) {
		baseDao.update(commoditySnap);
		return true;
	}

	@Override
	public boolean delCommoditySnap(CommoditySnap commoditySnap) {
		baseDao.delete(commoditySnap);
		return true;
	}

	@Override
	public CommoditySnap getCommoditySnap(Integer distribute, Integer commodity) {
		CommoditySnap commoditySnap = null;
		CommoditySnapPK snapPK = new CommoditySnapPK();
		snapPK.setDistribute(distribute);
		snapPK.setCommodity(commodity);
		final Map<String,Object> params = new HashMap<String,Object>();
		params.put("snapPK", snapPK);
		Finder finder = new ParametersFinder(params) {
			@Override
			public String genQueryString() {
				StringBuffer sb=new StringBuffer("SELECT s FROM CommoditySnap s WHERE 1=1");
					sb.append(" AND s.commoditySnapPK=:snapPK ");
				return sb.toString();
			}
		};
		List<CommoditySnap> snaps = baseDao.find(finder);
		if(snaps!=null && snaps.size()>0){
			commoditySnap = snaps.get(0);
		}
		return commoditySnap;
	}
	
	private BaseDao baseDao;

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public List<CommoditySnap> searchByMap(Map<String, Object> params,Pagination pagination) {
		final Map<String,Object> params2 = params;
		Finder finder = new ParametersFinder(params) {
			@Override
			public String genQueryString() {
				StringBuffer sb=new StringBuffer("SELECT s FROM CommoditySnap s WHERE 1=1");
				Integer distribute = (Integer)params2.get("distribute");
				if( distribute!= null && distribute != -1){
					sb.append(" AND s.commoditySnapPK.distribute=:distribute ");
				}
				Integer commodity = (Integer)params2.get("commodity");
				if(commodity != null && commodity != 0){
					sb.append(" AND s.commoditySnapPK.commodity=:commodity ");
				}
				String distributeCommodity = (String)params2.get("distributeCommodity");
				if(distributeCommodity != null && !StringUtils.isNullOrEmpty(distributeCommodity)){
					sb.append(" AND s.distributeCommodity=:distributeCommodity ");
				}
				String name = (String)params2.get("name");
				if(name != null && !StringUtils.isNullOrEmpty(name)){
					sb.append(" AND s.name = :name ");
				}
				String coversdigest = (String)params2.get("coversdigest");
				if(coversdigest != null && !StringUtils.isNullOrEmpty(coversdigest)){
					sb.append(" AND s.coversdigest=:coversdigest ");
				}
				return sb.toString();
			}
		};
		return baseDao.find(finder,pagination);
	}

	@Override
	public List<CommoditySnap> getCommoditySnapList(Finder finder,
			Pagination pagination) {
		return baseDao.find(finder, pagination);
	}
	

}
